package za.ac.wits.elen7045.persistence.datasource;

import java.util.LinkedHashMap;
import java.util.Map;
import za.ac.wits.elen7045.persistence.util.ValidationUtilities;

public class DataSourceRowEntry {
	
	private Map<String, String> map;
	
	public DataSourceRowEntry() {
		map = new LinkedHashMap<String, String>();
	}

	public Map<String, String> getValues() {
		return map;
	}

	public void setValues(Map<String, String> values) {
		this.map = values;
	}
	
	public String getValue(String name) {
		return name != null ? map.get(name) : null;
	}

	public void setValue(String name, String value) {
		map.put(name, value);
	}
	public void setValue(String name, int value) {
		map.put(name, Integer.toString(value));
	}
	public void setValue(String name, double value) {
		map.put(name, Double.toString(value));
	}
	
	public void removeValue(String name) {
		if (map.containsKey(name)) {
			map.remove(name);
		}
	}

	/**
	 * @param seperator - text value to separate entry-values
	 * @return string of values separated by the specified separator
	 */
	public String toString(String seperator) {
		StringBuffer buffer = new StringBuffer();
		int size = map.size();
		int index = 0;
		for (String name : map.keySet()) {
			buffer.append(map.get(name));
			buffer.append(index < size-1 ? seperator : "");
			index++;
		}
		return buffer.toString();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		} else if (obj instanceof DataSourceRowEntry) {
			DataSourceRowEntry dseObj = (DataSourceRowEntry) obj;
			if (map.size() == dseObj.map.size()) {
				boolean isEqual = true;
				for (String name : map.keySet()) {
					if (!ValidationUtilities.isEqual(map.get(name), dseObj.map.get(name))) {
						isEqual = false;
						break;
					}
				}
				return isEqual;
			}
		}
		return false;
	}
	
}
